import dept_request from "@/axios/dept"
import client_request from "@/axios/client"
import student_request from "@/axios/student"
import barJs from '@guide/js/vuex'
import store from '@/store'

export default {
    /**
     * Axios 请求
     * 获取部门、组别映射
     * @param {Boolean} force 是否强制请求 default: false
     */
    async axiosGetDept(force = false) {
        if (force || store.state.info.depts.length == 0) {
            store.state.info.depts = await dept_request.axiosGetDept()
            if (store.state.user != "")
                this.axiosStudent()
        }
    },
    /**
     * Axios 请求
     * 获取学院名称
     * @param {Boolean} force 是否强制请求 default: false
     */
    async axiosColleges(force = false) {
        if (force || store.state.info.colleges.length == 0)
            store.state.info.colleges = await client_request.axiosColleges()
    },
    /**
     * 遍历每个部门，获取该部分的学生信息
     * @param {Array} department_list 部门数组
     */
    async axiosStudent(department_list = store.state.info.depts) {
        for (let index = 0; index < department_list.length; index++) {
            const item = department_list[index]
            const group_list = item.groupList
            let result = await student_request.getStudentByDept(item.deptName)
            group_list.forEach(item => {
                let group = this.getGroup(result, item.groupName)
                if (group !== undefined)
                    item.studentList = group.studentList
            })
        }
    },
    /**
     * 从本地缓存读取数据，放入 Vuex
     */
    readStore() {
        if (sessionStorage.getItem("store")) {
            store.replaceState(Object.assign({}, store.state, JSON.parse(sessionStorage.getItem("store"))))
        }
        this.axiosGetDept()
        this.axiosColleges()
    },
    /**
     * 是否为管理员设备
     */
    isAdministrator() {
        return true
    },
    /**
     * 分页
     * @param {Array} list 待分页数据
     * @param {Number} index 开始位置
     * @param {Number} size 数据个数
     */
    dividePages(list, index, size) {
        let start = (index - 1) * size
        let end = index * size
        return list.slice(start, end)
    },
    /**
     * 获取指定 "名称" 的部门
     * @param {String} name 部门名称
     */
    getDept(dept_list = store.state.info.depts, name) {
        for (let index = 0; index < dept_list.length; index++) {
            const item = dept_list[index]
            if (item.deptName == name)
                return item
        }
    },
    /**
     * 从 "小组数组" 里获取指定 "名称" 的小组
     * @param {Array} group_list 小组数组
     * @param {String} name 小组名称
     */
    getGroup(group_list, name) {
        for (let index = 0; index < group_list.length; index++) {
            const item = group_list[index]
            if (item.groupName == name)
                return item
        }
    },
    /**
     * 从 "学生数组" 里获取指定 "学号" 的学生
     * @param {Array} student_list 学生数组
     * @param {String} sid 学号
     */
    getStuByGroup(student_list, sid) {
        for (let index = 0; index < student_list.length; index++) {
            const item = student_list[index]
            if (item.sid === sid)
                return item
        }
    },
    // 从本地获取学生信息
    getStudent(sid, dept_name, group_name, dept_list) {
        let department = this.getDept(dept_list, dept_name)
        let group = this.getGroup(department.groupList, group_name)
        return this.getStuByGroup(group.studentList, sid)
    },
    /**
     * 如果当前登录的用户是 "部长" 或者 "副部长"，则向菜单中添加 "部门管理"
     * @param {Object} user 当前登录的用户
     */
    addMenu(user) {
        if ((user.sid !== "") && (user.group === "部长" || user.group === "组长") && (barJs.state.mainMenus.length === 3)) {
            barJs.state.mainMenus.push({
                path: "/department",
                context: "部门管理",
                subMenus: [
                    {
                        path: "/department/manage",
                        context: "记录管理"
                    },
                    {
                        path: "/department/update",
                        context: "修改信息"
                    }
                ]
            })
        }
    },
    /**
     * 将数组Json化，并去掉双引号，以便后台接收
     * @param {Array} array 待转换数组
     */
    parseArray(array) {
        let string = JSON.stringify(array);
        let result = string.substring(1, string.length - 1);
        return result.replace(/"/g, "");
    },
    getImage(target, is_center = false) {
        let result = {
            background: `url(/img/${target})`,
            backgroundSize: `cover`
        }
        if (is_center) {
            result.backgroundPosition = `center`
        }
        return result
    }
}